import * as THREE from 'three'
import { GangTi } from './GangTi.js'
import { KongZhiTi } from './KongZhiTi.js'
import { wuTiCaoZuo, wuLiWorker } from '../../wuLi/WuLiWorker.js'

export class Che extends KongZhiTi(GangTi) {
	wuTiLeiXing = '车'
	shiChe = true
	chiCun = null // 底盘尺寸
	lunZi = [] // 轮子模型
	qian = false
	hou = false
	zuo = false
	you = false
	shouSha = false
	
	constructor(moXing, pengZhuangTi, chiCun) {
		super(moXing, pengZhuangTi, 800)
		
		this.chiCun = chiCun
		
		this.lunZiMoXing()
	}
	// 生成轮子模型
	lunZiMoXing() {
		let { x, y, z } = this.chiCun
		
		let createWheelMesh = (radius, width) => {
			var t = new THREE.CylinderGeometry(radius, radius, width, 24, 1);
			t.rotateZ(Math.PI / 2);
			var mesh = new THREE.Mesh(t, new THREE.MeshStandardMaterial({ color: 0x222222 }));
			mesh.castShadow = true
			mesh.receiveShadow = true

			mesh.userData.huoQuWuTi = () => this
			mesh.userData.shiLunZi = true
			mesh.userData.buCunDang = true

			this.lunZi.push(mesh)
		}
		createWheelMesh(y * 2, y * 1.5) // 左前
		createWheelMesh(y * 2, y * 1.5) // 右前
		createWheelMesh(y * 2, y * 1.5) // 左后
		createWheelMesh(y * 2, y * 1.5) // 右后
	}
	// 设置车
	sheZhiChe(sheZhiCanShu) {
		if (!this.pengZhuangZhuangTai) return
		let caoZuoShuZu = wuTiCaoZuo[this.bianHao]

		if (caoZuoShuZu) {
			let sheZhiCaoZuo = caoZuoShuZu.find(caoZuo => caoZuo.mingZi == 'sheZhiWuTi')
			if (sheZhiCaoZuo) {
				let canShu = sheZhiCaoZuo.canShu
				canShu.sheZhiChe = true
				canShu.qian = Boolean(sheZhiCanShu.qian)
				canShu.hou = Boolean(sheZhiCanShu.hou)
				canShu.zuo = Boolean(sheZhiCanShu.zuo)
				canShu.you = Boolean(sheZhiCanShu.you)
				canShu.shouSha = Boolean(sheZhiCanShu.shouSha)
			} else {
				let caoZuo = {
					mingZi: 'sheZhiWuTi',
					canShu: {
						bianHao: this.bianHao,
						mingZi: this.mingZi,
						sheZhiChe: true,
						qian: Boolean(sheZhiCanShu.qian),
						hou: Boolean(sheZhiCanShu.hou),
						zuo: Boolean(sheZhiCanShu.zuo),
						you: Boolean(sheZhiCanShu.you),
						shouSha: Boolean(sheZhiCanShu.shouSha)
					}
				}
				
				wuTiCaoZuo[this.bianHao].push(caoZuo)
			}
		}
	}

	tongBu(wuTiCanShu) {
		if (!this.tongBuWuLi || wuLiWorker.tingZhiMoNiZhuangTai) return
		
		let tb = (canShu, moXing) => {
			let { weiZhi, xuanZhuan } = canShu
			const thQ = new THREE.Quaternion()
			const thQ2 = new THREE.Quaternion()
			const thQ3 = new THREE.Quaternion()
			thQ.set(...xuanZhuan)
			thQ2.setFromRotationMatrix( moXing.matrixWorld )
			thQ2.conjugate()
			thQ2.multiply( thQ )
			thQ3.setFromRotationMatrix( moXing.matrix )
			moXing.quaternion.copy(thQ3.multiply( thQ2 ).normalize()) // 右乘左乘 此处写的不确定
			
			// 同步位置
			// 步进模拟不是requestAnimationFrame驱动，而是物理世界独立setInterval时，车的位置有抖动
			let xiangLiang = new THREE.Vector3()
			xiangLiang.set(...weiZhi)
			moXing.position.copy(moXing.parent.worldToLocal(xiangLiang))
		}
		
		tb(wuTiCanShu, this.moXing)
		this.lunZi.forEach((l, i) => tb(wuTiCanShu.lunZi[i], l))
	}
	// 默认快捷键
	moRenKuaiJieJian() {
		this.kuaiJieJian = {
			qianJin: {
				miaoShu: '前进',
				kuaiJieJian: 'ArrowUp'
			},
			houTui: {
				miaoShu: '后退',
				kuaiJieJian: 'ArrowDown'
			},
			zuoZhuan: {
				miaoShu: '左转',
				kuaiJieJian: 'ArrowLeft'
			},
			youZhuan: {
				miaoShu: '右转',
				kuaiJieJian: 'ArrowRight'
			},
			shouSha: {
				miaoShu: '手刹',
				kuaiJieJian: 'End'
			}
		}
	}

	jianPanXia(event) {
		let kjj = this.kuaiJieJian
		let jiShu = 0
		switch (event.code) {
			case kjj.qianJin.kuaiJieJian:
				this.qian = true;
				jiShu ++
				break;
			case kjj.houTui.kuaiJieJian:
				this.hou = true;
				jiShu ++
				break;
			case kjj.zuoZhuan.kuaiJieJian:
				this.zuo = true;
				jiShu ++
				break;
			case kjj.youZhuan.kuaiJieJian:
				this.you = true;
				jiShu ++
				break;
			case kjj.shouSha.kuaiJieJian:
				this.shouSha = !this.shouSha;
				jiShu ++
				break;
		}
		
		if (jiShu > 0) this.sheZhiChe({ qian: this.qian, hou: this.hou, zuo: this.zuo, you: this.you, shouSha: this.shouSha })
	}
	
	jianPanShang(event) {
	let kjj = this.kuaiJieJian
	let jiShu = 0
		switch (event.code) {
			case kjj.qianJin.kuaiJieJian:
				this.qian = false;
				jiShu ++
				break;
			case kjj.houTui.kuaiJieJian:
				this.hou = false;
				jiShu ++
				break;
			case kjj.zuoZhuan.kuaiJieJian:
				this.zuo = false;
				jiShu ++
				break;
			case kjj.youZhuan.kuaiJieJian:
				this.you = false;
				jiShu ++
				break;
		}
		
		if (jiShu > 0) this.sheZhiChe({ qian: this.qian, hou: this.hou, zuo: this.zuo, you: this.you, shouSha: this.shouSha })
	}
	
	chuLi(shiJian) {
		if (shiJian.type == 'keydown') this.jianPanXia(shiJian)
		if (shiJian.type == 'keyup') this.jianPanShang(shiJian)
	}
	
	jiaZai(fuMoXing) {
		super.jiaZai(fuMoXing)
		
		this.lunZi.forEach(l => this.moXing.add(l))
		
		this.sheZhiChe({ shouSha: this.shouSha })
	}
	
	xieZai() {
		super.xieZai()
		
		this.lunZi.forEach(l => l.removeFromParent())
	}
}